package com.ice.netty.modbus.data;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

public class DeviceInfoData extends MiiServerBaseData {

    private static final Charset DEFAULT_CHARSET = Charset.forName("GBK");

    private String deviceNo;

    public DeviceInfoData(byte[] datas) {
        super(datas);

        this.decode();
    }

    private void decode() {
        deviceNo = this.contentStr;
    }

    @Override
    public byte[] toBytes() {

        byte[] commond = new byte[] {(byte)20, (byte)01, (byte)00, (byte)00};
        byte[] bytes = toBytes(deviceNo);

        byte[] msg = new byte[commond.length + bytes.length];

        System.arraycopy(commond, 0, msg, 0, commond.length);
        System.arraycopy(bytes, 0, msg, commond.length, bytes.length);
        return msg;
    }

    public byte[] toBytes(String... contents) {
        List<Byte> resList = new ArrayList<>();
        for (int i = 0; i < contents.length; i++) {
            String content = contents[i];
            Collections.addAll(resList, ArrayUtils.toObject(content.getBytes(DEFAULT_CHARSET)));
        }
        return ArrayUtils.toPrimitive(resList.toArray(new Byte[resList.size()]));
    }

    public String getDeviceNo() {
        return deviceNo;
    }

    public void setDeviceNo(String deviceNo) {
        this.deviceNo = deviceNo;
    }
}
